package com.app.mingluxing.my;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.app.mingluxing.R;
import com.app.mingluxing.base.BaseActivity;
import com.app.mingluxing.bean.BabyDetailsResult;
import com.app.mingluxing.net.HttpUtil;
import com.app.mingluxing.utils.CommonUtils;
import com.app.mingluxing.utils.Constant;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2015/9/9.
 */
public class BabyModifySexActivity extends BaseActivity{

    private ImageView cb_mal;
    private ImageView cb_femal;
    private String sex;
    private String id;
    private BabyDetailsResult.DataEntity babyInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_chang_sex);
        id = getIntent().getStringExtra("id");

        babyInfo = getIntent().getParcelableExtra("babyInfo");

        init();

    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {

            case R.id.iv_left:
                finish();
                break;

            case R.id.tv_right://完成
                upDateUserInfo();
                break;

            case R.id.rl_meal://男选中女不选中

                sex = "男";
                cb_mal.setSelected(true);
                cb_femal.setSelected(false);

                break;
            case R.id.rl_femal://女选中男不选中
                sex = "女";
                cb_mal.setSelected(false);
                cb_femal.setSelected(true);

                break;

        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mApplication.cancelPendingRequests(getClass().getName());
    }

    private void init() {

        ImageView tv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        TextView tv_right = (TextView) findViewById(R.id.tv_right);

        tv_title.setText(getResources().getString(R.string.change_sex));
        tv_right.setText(getResources().getString(R.string.done));

        RelativeLayout rl_meal = (RelativeLayout) findViewById(R.id.rl_meal);
        RelativeLayout rl_femal = (RelativeLayout) findViewById(R.id.rl_femal);

        tv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);

        rl_meal.setOnClickListener(this);
        rl_femal.setOnClickListener(this);

        cb_mal = (ImageView) findViewById(R.id.cb_mal);
        cb_femal = (ImageView) findViewById(R.id.cb_femal);

        cb_mal.setSelected(true);
        cb_femal.setSelected(false);

        if (babyInfo == null) {
            return;
        }

        //
        if (Constant.FREMAL.equals(babyInfo.sex)) {
            cb_mal.setSelected(false);
            cb_femal.setSelected(true);
        } else {
            cb_mal.setSelected(true);
            cb_femal.setSelected(false);
        }
    }

    /**
     * 提交用户修改的信息
     */
    private void upDateUserInfo() {

        if (CommonUtils.isNull(sex)) {
            showToast("请先选择性别");
            return;
        }

        JSONObject j = new JSONObject();
        j.put("id", id);
        j.put("token", sp.getString(Constant.TOKEN, ""));
        j.put("birthDay", babyInfo.birthDay);
        j.put("name", babyInfo.childName);
        j.put("sex", sex);
        j.put("photo", babyInfo.picUrl);

        HttpUtil.setPostRequest("更新中...", this, Constant.WEB_ROOT + Constant.UPDATECHILDINFO,
                j.toJSONString(), getClass().getName(), new HttpUtil.SuccessListener() {

                    @Override
                    public void setOnSuccessListener(String str) {

                        try {
                            org.json.JSONObject result = new org.json.JSONObject(str);
                            if (result.getInt("code") == Constant.STATUS_SUCCESS) {

                                babyInfo.sex = sex;
                                EventBus.getDefault().post(babyInfo);

                                finish();
                            } else {

                                showToast(Constant.showErrorMsg(result.getInt("code"), BabyModifySexActivity.this));

                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            showToast(Constant.NODATA);

                        }
                    }
                }, null);
    }
}



